package com.minelx.sequence.pattern;

import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sequence.core.StringScanner;

public class PrefixMatcher<E> implements ReturnMatcher<E, StringScanner> {
	private final ReturnMatcher<String, StringScanner> prefixMatcher;

	private final ReturnMatcher<E, StringScanner> elementMatcher;

	public PrefixMatcher(ReturnMatcher<String, StringScanner> prefixMatcher, ReturnMatcher<E, StringScanner> elementMatcher) {
		this.prefixMatcher = prefixMatcher;
		this.elementMatcher = elementMatcher;
	}

	@Override
	public boolean isMatch(StringScanner clue) {
		return prefixMatcher.isMatch(clue);
	}

	@Override
	public E onMatched(StringScanner clue) {
		prefixMatcher.onMatched(clue);
		return elementMatcher.get(clue).orElseThrow(RuntimeException::new);
	}
}
